package com.bstek.consume.controller;

import com.bstek.consume.bean.Depart;
import com.bstek.consume.service.DepartService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Derek.Zhang
 * @Title:
 * @Package
 * @Description:
 * @date 2020/2/26 8:32
 */
@RestController
@RequestMapping("/consumer/depart")
public class DepartController {

	@Autowired
	private DepartService service;

	@PostMapping("/save")
	public boolean saveHandle(Depart depart) {
		return service.saveDepart(depart);
	}

	@DeleteMapping("/del/{id}")
	public boolean deleteHandle(@PathVariable("id") int id) {
		return service.removeDepartById(id);
	}

	@PutMapping("/update")
	public boolean updateHandle(Depart depart) {
		return service.modifyDepart(depart);
	}

	//1--- 服务降级：若当前处理器方法发生异常，则执行fallbackMethod属性指定的方法

	//2--为了能够看到降级处理类的响应结果，可以直接将消费者处理中的@HystrixCommand
	//2--注释掉，即去除了 fallbackMethod 的降级处理方式。
	@HystrixCommand(fallbackMethod = "getHystrixHandle")
	@GetMapping("/get/{id}")
	public Depart getHandle(@PathVariable("id") int id) {
		System.out.println("执行getDepartById()的服务降级处理方法1");
		Depart depart = service.getDepartById(id);
		System.out.println("执行getDepartById()的服务降级处理方法2");
		if(depart == null) {
			throw new RuntimeException("no this depart：" + id);
		}
		return depart;
	}


	public Depart getHystrixHandle(@PathVariable("id") int id) {
		Depart depart = new Depart();
		depart.setId(id);
		depart.setName("no this depart");
		depart.setDepartInfo("no this info");
		return depart;
	}


	@GetMapping("/list")
	public List<Depart> listHandle() {
		return service.listAllDeparts();
	}
}
